package userInterface;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class FileReader465
{
	private static String popFrontValue(String str)
	{
		return str.substring(str.indexOf(' ') + 1);
	}
		
	private static String getFrontValue(String str)
	{
		if (str.indexOf(' ') != -1)
			return str.substring(0, str.indexOf(' '));
		return str;
	}
	
	private static Point loadPoint(String desc)
	{
		String x = getFrontValue(desc);
		desc = popFrontValue(desc);
		String y = getFrontValue(desc);
		desc = popFrontValue(desc);
		
		return new Point(x, y);
	}
	
	private static Color loadColor(String desc)
	{
		// get the first color
		String red = getFrontValue(desc);
		desc = popFrontValue(desc);
		String green = getFrontValue(desc);
		desc = popFrontValue(desc);
		String blue = getFrontValue(desc);
		desc = popFrontValue(desc);
		
		return new Color(
				Integer.parseInt(red),
				Integer.parseInt(green),
				Integer.parseInt(blue));
	}

	public static DrawObject getObject(String line)
	{
		DrawObject retObj = null;
		switch (line.charAt(0))
		{
		case 'L':
			 retObj = parseLine(line);
			 break;
		case 'R':
			retObj = parseRectangle(line);
			break;
		case 'E':
			retObj = parseEllipse(line);
			break;
		case 'B':
			retObj = parseBrzier(line);
			break;
		case 'T':
			retObj = parseText(line);
			break;
		}

		return retObj;
	}
		
	private static Line parseLine(String desc)
	{
		desc = popFrontValue(desc);
	
		// get the first point
		Point start = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);

		// get the second point
		Point end = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);
		
		// get the thickness
		int thick = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);

		// get the color
		Color color = loadColor(desc);
		
		return new Line(start, end, thick, color);
	}
	
	private static Rectangle parseRectangle(String desc)
	{
		desc = popFrontValue(desc);
		
		// get the top left point
		Point topLeft = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);
		
		// get the width
		int width = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);

		// get the height
		int height = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);
		
		// get the thickness
		int thick = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);

		Color color = loadColor(desc);
		
		return new Rectangle(topLeft, width, height, thick, color);
	}

	private static Ellipse parseEllipse(String desc)
	{
		desc = popFrontValue(desc);
		
		// get the top left point
		Point topLeft = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);
		
		// get the width
		int width = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);

		// get the height
		int height = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);
		
		// get the thickness
		int thick = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);

		Color color = loadColor(desc);
		
		return new Ellipse(topLeft, width, height, thick, color);
	}

	private static Curve parseBrzier(String desc)
	{
		desc = popFrontValue(desc);
		
		// get point 1
		Point start = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);

		// get point 2
		Point ctl1 = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);

		// get point 3
		Point ctl2 = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);

		// get point 4
		Point end = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);

		// get the thickness
		int thickness = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);

		// get the color
		Color color = loadColor(desc);
		
		return new Curve(start, ctl1, ctl2, end, thickness, color);
	}

	private static Text parseText(String desc)
	{
		desc = popFrontValue(desc);
		
		// get the location
		Point loc = loadPoint(desc);
		desc = popFrontValue(desc);
		desc = popFrontValue(desc);
		
		// get the thickness
		int fontSize = Integer.parseInt(getFrontValue(desc));
		desc = popFrontValue(desc);
		
		// get the font
		String font = desc.substring(1, desc.lastIndexOf('"'));
		desc = desc.substring(desc.lastIndexOf('"') + 2);
		
		// get the color
		Color color = loadColor(desc);
		
		return new Text(loc, fontSize, font, color);
	}
	
	public static ArrayList<DrawObject> readFile(String filename)
	{
		ArrayList<DrawObject> objects = new ArrayList<DrawObject>();
		try
		{
			FileInputStream fin = new FileInputStream(filename);
			DataInputStream in = new DataInputStream(fin);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			
			while(br.ready())
			{
				String line = br.readLine();
				objects.add(getObject(line));
			}
			
			br.close();
			in.close();
			fin.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return objects;
	}
}
